
#include "PreHeader.h"
#include "cGameHUDLayer.h"

#include "cMsgManager.h"
#include "cLogicDataManager.h"
#include "MsgGameLogic.h"

cGameHUDLayer::cGameHUDLayer()
{
	m_scoreBar = NULL;
	m_score = 0;
	m_prevScore = 0;
	m_scoreBarWidth = 0.0f;
	m_scoreVariation = 0;

	m_restartMenu = NULL;
	m_labelGameOver = NULL;
	m_labelStageClear = NULL;
}

cGameHUDLayer::~cGameHUDLayer()
{

}

bool cGameHUDLayer::init()
{
	scheduleUpdate();

	m_scoreBar = CCSprite::spriteWithFile("ScoreBarUI.png");
	RETURN_FALSE_IF_NULL(m_scoreBar);
	addChild(m_scoreBar);

	const nConstantFormat01::sConstant* constant = cLogicDataManager::it()->GetConstantData();

	// init score bar
	m_scoreBarWidth = constant->HUD.scoreBar.width;
	m_scoreBar->setPosition(ccp(constant->HUD.scoreBar.positionX, constant->HUD.scoreBar.positionY));
	m_scoreBar->setAnchorPoint(ccp(0.0f, 0.0f));
	m_scoreBar->setScaleY(constant->HUD.scoreBar.height);
	m_scoreBar->setScaleX(0.0f);

	// init restart menu
	CCMenuItemFont* itemRestart = CCMenuItemFont::itemFromString("Restart?", this, menu_selector(cGameHUDLayer::OnRestart));
	m_restartMenu = CCMenu::menuWithItems(itemRestart, NULL);
	m_restartMenu->retain();

	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

	// game over label
	m_labelGameOver = CCLabelTTF::labelWithString("Game Over", "Thonburi", 34.0f);
	CCSize size = m_labelGameOver->getContentSize();
	tFloat positionX = screenSize.width - size.width;
	m_labelGameOver->setPosition(ccp(positionX, 300.0f));
	m_labelGameOver->retain();

	// stage clear label
	m_labelStageClear = CCLabelTTF::labelWithString("Stage Clear", "Thonburi", 34.0f);
	size = m_labelGameOver->getContentSize();
	positionX = screenSize.width - size.width;
	m_labelStageClear->setPosition(ccp(positionX, 300.0f));
	m_labelStageClear->retain();

	return true;
}

void cGameHUDLayer::update(ccTime dt)
{
	if (m_scoreBarTimer.IsUsing() == true)
	{
		m_scoreBarTimer.Update(dt);
		if (m_scoreBarTimer.IsDone() == true)
		{
			m_scoreBarTimer.Clear();
		}
		else
		{
			tFloat v = cTweener::cExpo::Out(m_scoreBarTimer.GetTimer(), 0.0f, tFloat(abs(m_scoreVariation)), m_scoreBarTimer.GetDuration());
			if (m_scoreVariation > 0)
			{
				v = -v;
			}

			tFloat scale = m_scoreBarWidth * ((tFloat(m_prevScore) + v)/m_scoreBarWidth);

			m_scoreBar->setScaleX(scale);
		}
	}
}

void cGameHUDLayer::SetScore(tInt score)
{
	m_scoreVariation = m_score - score;
	if (m_scoreVariation == 0)
	{
		return;
	}

	m_prevScore = m_score;
	m_score = score;

	tFloat time = tFloat(abs(m_scoreVariation)) / 50.0f;
	m_scoreBarTimer.Init(0.3f);
}

void cGameHUDLayer::SetGameOver()
{
	setIsTouchEnabled(true);

	addChild(m_restartMenu, 1);
	addChild(m_labelGameOver);
}

void cGameHUDLayer::SetStageClear()
{
	setIsTouchEnabled(true);

	addChild(m_restartMenu, 1);
	addChild(m_labelStageClear);
}

void cGameHUDLayer::OnRestart(CCObject* pSender)
{
	setIsTouchEnabled(false);
	removeChild(m_restartMenu, false);
	removeChild(m_labelGameOver, false);
	removeChild(m_labelStageClear, false);

	nGameLogic::sMSG_Restart info;
	cMsgManager::it()->SendMsg(MSG_TARGET_LOGIC_MANAGER, info);
}

BEGIN_MSG_MAP(cGameHUDLayer, iMsgTargetBase)
	MSG_MAP(nGameLogic, MSG_ScoreChanged, Handle)
	MSG_MAP(nGameLogic, MSG_GameOver, Handle)
	MSG_MAP(nGameLogic, MSG_StageClear, Handle)
END_MSG_MAP()

void cGameHUDLayer::Handle(const nGameLogic::sMSG_ScoreChanged& msg)
{
	SetScore(msg.score);
}

void cGameHUDLayer::Handle(const nGameLogic::sMSG_GameOver& msg)
{
	SetGameOver();
}

void cGameHUDLayer::Handle(const nGameLogic::sMSG_StageClear& msg)
{
	SetStageClear();
}